package demo2;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @User: vitobo
 * @Date: 2024-05-27
 * @Description: 接口的使用实例
 *      如果我们 自定义的类型, 一定要记住 如果比较大小, 那么必须要这个类 具备可以比较的功能
 *
 *
 */



// Comparable 对类的侵入性非常强
// <Student> 泛型
class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Student o){
        if(this.age - o.age > 0){
            return 1;
        } else if (this.age - o.age < 0) {
            return -1;
        }else {
            return 0;
        }
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}


// 问题: Comparator接口当中 不止一个抽象方法, 那么 为什么只需要实现一个compare方法就行?
//      因为Comparator接口中的其他方法，比如equals和toString，都有默认实现，
//      所以你只需要关注compare方法来定义对象之间的比较规则即可

// Comparator 对类的侵入性比较弱, 比较器
// 另一个接口实现
class AgeComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}


class NameComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}

public class Test {
    public static void bubbleSort(Comparable[] array){
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j].compareTo(array[j+1]) > 0){
                    Comparable tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                }
            }
        }
    }

    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("bobo", 25);
        students[1] = new Student("gege", 31);
        students[2] = new Student("bibi", 15);

        bubbleSort(students);
        System.out.println(Arrays.toString(students));
    }


    public static void main4(String[] args) {
        Student student1 = new Student("bobo", 15);
        Student student2 = new Student("yuanyuan", 24);

        AgeComparator ageComparator = new AgeComparator();
        if(ageComparator.compare(student1, student2) > 0){
            System.out.println("student1>student2");
        }else{
            System.out.println("student1<student2");
        }
    }

    public static void main3(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("bobo", 25);
        students[1] = new Student("gege", 31);
        students[2] = new Student("bibi", 15);

        // AgeComparator ageComparator = new AgeComparator();
        NameComparator nameComparator = new NameComparator();


        Arrays.sort(students, nameComparator);
        System.out.println(Arrays.toString(students));
    }

    public static void main2(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("bobo", 25);
        students[1] = new Student("gege", 31);
        students[2] = new Student("bibi", 15);

        // 自定义的学生类,不要具备可以比较的功能

        // 类型转化异常
        Arrays.sort(students);
        System.out.println(Arrays.toString(students));
    }

    public static void main1(String[] args) {
        int[] array = {1,3,5,7,8,4};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }

}
